/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AdminOperations;

import CRUD.ThreadHome;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import xmlMapping.Thread;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ABDELMOHSEN
 */
public class AdminManageGuest {
    public void lockThread(int ThreadID){
        ThreadHome th=new ThreadHome();
        Thread t=th.findById(ThreadID);
        t.setLocked(true);
        th.persist(t);
    }

    public boolean isregistrationallowed(){
        boolean allowed=true;
        File f=new File("./ForumConfig.txt");
            try {
                DataInputStream din = new DataInputStream(new FileInputStream(f));
                int theme=din.readInt();
                if(din.readInt()==0) allowed=false;
            } catch (FileNotFoundException ex) {
                try {
                    DataOutputStream dOut = new DataOutputStream(new FileOutputStream(f));
                    dOut.writeInt(1);
                    dOut.writeInt(1);
                } catch (IOException ex1) {
                    Logger.getLogger(AdminManageGuest.class.getName()).log(Level.SEVERE, null, ex1);
                }
            }
            catch (IOException ex) {
                Logger.getLogger(AdminManageTheme.class.getName()).log(Level.SEVERE, null, ex);
            }
        return allowed;
    }

    public void lockRegistration(boolean blocked){
        int setMe=1;
        if(blocked==true) setMe=0;
        File f=new File("./ForumConfig.txt");
        try {
            DataInputStream din = new DataInputStream(new FileInputStream(f));
            try {
                int theme=din.readInt();
                int registration=din.readInt();
                DataOutputStream dOut = new DataOutputStream(new FileOutputStream(f));
                dOut.writeInt(theme);
                dOut.writeInt(setMe);
            } catch (IOException ex) {
                Logger.getLogger(AdminManageTheme.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            try {
                DataOutputStream dOut = new DataOutputStream(new FileOutputStream(f));
                dOut.writeInt(setMe);
                dOut.writeInt(1);
            } catch (Exception ex1) {
                Logger.getLogger(AdminManageTheme.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }

    public void unLockThread(int ThreadID){
        ThreadHome th=new ThreadHome();
        Thread t=th.findById(ThreadID);
        t.setLocked(false);
        th.persist(t);
    }
    /*public static void main(String[] args){
        //new AdminManageGuest().lockThread(new ThreadHome().findById(2).getThreadId());
        new AdminManageGuest().lockRegistration(false);
        File f=new File("./ForumConfig.txt");
        try {
            DataInputStream din = new DataInputStream(new FileInputStream(f));
            int theme=din.readInt();
                int registration=din.readInt();
                System.out.println("***********"+theme);
                System.out.println("***********"+registration);
        }
        catch(Exception e){
            
        }
    }*/
}
